package me.leiusual.sharing.ui.mine

import android.os.Bundle
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import dagger.android.support.DaggerFragment
import kotlinx.android.synthetic.main.fragment_mine_renew.*
import me.leiusual.sharing.R
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.ui.helper.HandleObserver
import me.leiusual.sharing.ui.mine.adapter.MineRenewAdapter
import timber.log.Timber
import javax.inject.Inject

/**
 * author: leiusual
 * create on: 2019-08-14
 * description:
 */
class MineRenewFragment : DaggerFragment() {

  @Inject
  lateinit var factory: ViewModelProvider.Factory

  private val viewModel: MineRenewViewModel by viewModels { factory }

  private val args: MineRenewFragmentArgs by navArgs()

  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.fragment_mine_renew, container, false)
  }

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    toolbar.setNavigationOnClickListener {
      findNavController().popBackStack()
    }
    toolbar.setOnMenuItemClickListener {
      Timber.v("menuItem = %s, itemId = %s, groupId = %s", it, it.itemId, it.groupId)
      if (it.groupId == 1) {
        val data = viewModel.userRenewList.value as? Data.Success
        data?.data?.let { userRenew ->
          userRenew.businessTypeOptions.entries.find { entry ->
            entry.value == it.title
          }?.let { entry ->
            viewModel.userRenewList(entry.key, args.userId)
          }
        }
        true
      } else {
        false
      }
    }

    recycler_view.adapter = MineRenewAdapter()

    observeData()
    viewModel.userRenewList(null, args.userId)
  }

  private fun observeData() {
    viewModel.userRenewList.observe(viewLifecycleOwner, HandleObserver {
      Timber.v("userRenewList = %s", it)
      when (it) {
        is Data.Loading -> {
          state_constraint_layout.showProgress()
        }
        is Data.Error -> {
          state_constraint_layout.showError(it.error.message)

          // 失败了，把可选类别清除
          toolbar.menu.clear()
        }
        is Data.Success -> {
          state_constraint_layout.showContent()

          // 可选类别
          toolbar.menu.clear()
          val subMenu = toolbar.menu.addSubMenu(it.data.businessTypeOptions[it.data.curBusinessType] + ">")
          subMenu.item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
          it.data.businessTypeOptions.forEach { entry ->
            subMenu.add(1, 0, 0, entry.value)
          }

          // 列表
          val adapter = recycler_view.adapter as MineRenewAdapter
          adapter.submitList(it.data.renewHistories)
        }
      }
    })
  }
}